/*
 * @Author: jianfanfan 1217572833@qq.com
 * @Date: 2025-03-15 16:06:21
 * @LastEditors: jianfanfan 1217572833@qq.com
 * @LastEditTime: 2025-03-26 19:11:57
 * @FilePath: \guozaoke\src\app.module.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Module,
  OnModuleInit,
  MiddlewareConsumer,
  NestModule,
} from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import configuration from './config/configuration';
import { AuthModule } from './auth/auth.module';
import { NodeModule } from './node/node.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import { User } from './auth/entities/user.entity';
import { Node } from './node/entities/node.entity';
import { Post } from './node/entities/post.entity';
import { Reply } from './node/entities/reply.entity';
import { initialNodes } from './config/nodes.init';
import { CacheModule } from './common/cache.module';
import { LoggerModule } from './common/logger/logger.module';
import { HttpLoggerMiddleware } from './common/middleware/http-logger.middleware';

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: '.env',
      isGlobal: true,
      load: [configuration],
    }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: process.env.DATABASE_HOST,
      port: parseInt(process.env.DATABASE_PORT || '3306', 10),
      username: process.env.DATABASE_USER,
      password: process.env.DATABASE_PASSWORD,
      database: process.env.DATABASE_NAME,
      entities: [User, Node, Post, Reply],
      synchronize: true,
      logging: ['error'],
    }),
    LoggerModule,
    AuthModule,
    NodeModule,
    CacheModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  // 配置中间件
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(HttpLoggerMiddleware).forRoutes('*'); // 应用到所有路由
  }
}
